$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var workYD = JSON.parse(localStorage.getItem("workYD"));
    var pass = 1;

    //点击返回箭头 清除 缓存 打开 运单页面
    $(".head .goBack").on("click", function () {
        localStorage.removeItem("workYD");
        localStorage.removeItem("work");
        window.location.href = "deliverYD.html";
    });

    $("#goods_id").focus();

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "deliver.html";
    });

    //获取发货单明细列表
    //清空页面中的发货单
    $("#work_list").find("li").remove();
    //加载提示符 显示
    $(".content .list-block ul#work_list .infinite-scroll-preloader").show();
    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        url: url + "/app/deliver/getSaleoutorderdetails",
        type: "post",
        data: {
            _id: workYD.id
        },
        dataType: 'json',
        json: 'callback',
        cache: false,
        timeout: 60000,
        success: function (res) {
            if (res.success) {
                var data = res.result;
                //清空页面中的发货单
                $("#work_list").find("li").remove();
                //隐藏提示符 显示
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                //console.log(data);
                if (data.length == 0) return $alert("运单明细不存在");
                var data_html = '';
                for (var i = 0; i < data.length; i++) {
                    data_html += '<li>' +
                        '<p class="li_title">运单明细号：<span class="sp work_code">' + data[i].bill_Num + '</span><span class="icon iconfont icon-right pull-right"></span></p>' +
                        '<div class="row no-gutter li_content"><div class="col-100">' +
                        '<p class="p_none"><span>id:</span><span class="sp goods_id">' + data[i]._id + '</span></p>' +
                        '<div class="details">' +
                        '<p><span>车牌:</span><span class="sp goods_plate_no">' + data[i].plate_Number + '</span>' +
                        '<span class="sp goods_driver_name">' + data[i].driver_Name + '</span>' +
                        '<span class="sp goods_status">' + data[i].status + '</span></p>' +
                        '<p><span>发货仓库:</span><span class="sp goods_warehouseName">' + data[i].de_Ware_Name + '</span></p>' +
                        '<p><span>客户名称:</span><span class="sp goods_customerName">' + data[i].re_Company_Name + '</span></p>' +
                        '<p><span>物料编码:</span><span class="sp goods_productCode">' + data[i].inv_Code + '</span></p>' +
                        '<p><span>物料名称:</span><span class="sp goods_productName">' + data[i].inv_Name + '</span></p>' +
                        '<p><span>装货方式:</span><span class="sp goods_specialreq">' + data[i].specialreq + '</span></p>' +
                        '<p><span>发货件数:</span><span class="sp goods_pre_Nums">' + (data[i].pre_Nums || "") + '</span>' +
                        '<span>发货数量:</span><span class="sp goods_pre_Weight">' + (data[i].pre_Weight || "") + '</span>T</p>' +
                        '<p><span>剩余发货件数:</span><span class="sp goods_planCount">' + (data[i].planCount || "") + '</span></p>' +
                        '<p><span>制单日期:</span><span class="sp goods_createDate">' + data[i].create_Date + '</span></p>';
                    if (data[i].fentry_remark) {
                        data_html += '<p><span>备注:</span><span class="sp comment">' + data[i].fentry_remark + '</span></p>';
                    }
                    data_html += '</div></div></div></li>';
                }
                $("#work_list").append(data_html);
                //改变 共计 数
                $(".content div.ul_foot span.sp_js").html($("#work_list li").length);
            } else {
                //隐藏提示符 显示
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                $alert(res.message);
            }
        },
        error: function (error) {
            //隐藏提示符 显示
            $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
            if (error.status == 0) {
                $alert("请求超时，请再试一次！");
            } else {
                console.log(error);
                $alert(error.responseText);
            }
        }
    });

    //点击 发货单箭头
    $(".content #work_list").on("click", "li .li_title", function () {
        var id = $(this).parent().find("span.goods_id").html();
        var code = $(this).parent().find("span.work_code").html();
        var productName = $(this).parent().find("span.goods_productName").html();
        var planCount = $(this).parent().find("span.goods_planCount").html();
        loading_show("选择中...");
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + "/app/deliver/selectSaleoutorderdetail",
            data: {
                _id: id
            },
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                loading_hide();
                if (res.success) {
                    var data = res.result;
                    var work = {
                        locationInventorys: data,
                        id: id,
                        code: code,
                        productName: productName,
                        planCount: planCount
                    };
                    localStorage.setItem("work", JSON.stringify(work));
                    window.location.href = "deliver_one.html";
                } else {
                    //隐藏提示符 显示
                    $alert(res.message);
                }
            },
            error: function (error) {
                loading_hide();
                if (error.status == 0) {
                    //loading_hide();
                    $alert("请求超时，请重新操作！");
                } else {
                    //loading_hide();
                    $alert(error.responseText);
                }
            }
        });
    });

    //点击 装车
    $('.foot .add_success').on("click", function () {
        var len = $("#work_list li").length;
        var html = '';
        for (var i = 0; i < len; i++) {
            var productName = $("#work_list li").eq(i).find("span.goods_productName").html();
            var planCount = $("#work_list li").eq(i).find("span.goods_planCount").html();
            if (planCount > 0) {
                html += `<p>${productName}剩余发货件数${planCount}</p>`;
            }
        }
        html += "确认装车完成？";
        $.confirm(html, function () {
            if (pass == 0) {
                return
            }
            pass = 0;
            loading_show("装车中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + "/app/deliver/loadSaleoutorder",
                data: {
                    _id: workYD.id
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                timeout: 60000,
                success: function (res) {
                    loading_hide();
                    pass = 1;
                    if (res.success) {
                        $alert("装车完成！");
                        setTimeout(function () {
                            localStorage.removeItem("workYD");
                            localStorage.removeItem("work");
                            window.location.href = "deliverYD.html";
                        }, 1400);
                    } else {
                        //隐藏提示符 显示
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    if (error.status == 0) {
                        //loading_hide();
                        $alert("请求超时，请重新操作！");
                    } else {
                        //loading_hide();
                        $alert(error.responseText);
                    }
                }
            });
        });
    });

});